package com.example.thread.d9CompletableFuture2.d3异步任务回调;

import com.example.thread.d9CompletableFuture2.CommonUtils;

import java.util.Arrays;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class D5ThenApplyAsyncDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CommonUtils.printThreadLog("main start");

        ExecutorService pool = Executors.newFixedThreadPool(5);

        CompletableFuture<String[]> future = CompletableFuture.supplyAsync(() -> {
            CommonUtils.printThreadLog("读取文件开始");
            String s = CommonUtils.readFile("读取,文件");
            return s;
        }).thenApplyAsync(content -> {
            CommonUtils.printThreadLog("打印输出");
            String[] contents = content.split(",");
            return contents;
        },pool);

        CommonUtils.printThreadLog("main continue");
        String[] strings = future.get();
        CommonUtils.printThreadLog("打印。。"+ Arrays.toString(strings));
        CommonUtils.printThreadLog("main end");

        /**
         * thenAccept和其异步回调
         * CompletableFuture<void> thenAccept(Consumer<T> action)
         * CompletableFuture<void> thenAcceptAsync(Consumer<T> action)
         * CompletableFuture<void> thenAcceptAsync(Consumer<T> action,Executor executor)
         *
         * thenRun和其异步回调
         * CompletableFuture<void> thenRun(Runnable action)
         * CompletableFuture<void> thenRunAsync(Runnable action)
         * CompletableFuture<void> thenRunAsync(Runnable action,Executor executor)
         */
    }
}
